<?php

function get_template($typeTpl, $params = null) {
    $html = null;
    switch ($typeTpl) {
        case 0:
            $html = get_listuser();
            break;
        case 1:
            $html = get_frm_add_user();
            break;
        case 2:
            if ($params != null && is_array($params)) {
                $userId = $params['userId'];
                $html = get_frm_update($userId);
            }
            break;
        default:
            break;
    }
    return $html;
}

function get_frm_update($userId) {
    global $tpl, $az_db;
    $file_tpl = 'user/frm_edit_user';
    $sql_P = "SELECT * FROM az_users WHERE user_id=" . $userId;
    $result_P = $az_db->executeSQL_Query($sql_P);
    $html = null;
    if ($result_P->num_rows > 0) {
        $rows = $result_P->fetch_array(MYSQLI_ASSOC);
        $html = $tpl->get_main($file_tpl, 2);
        $html = $tpl->get_vars($html, array('username' => $rows['username'], 'password' => $rows['password'], 'name' => $rows['name'],
                    'phone' => $rows['phone'], 'address' => $rows['address'], 'email' => $rows['email'],
                    'birthday' => $rows['birthday'], 'userId' => $userId));
    }
    $result_P->free();
    return $html;
}

//Function get template for add new user
function get_frm_add_user() {
    global $tpl ,$az_db;
    $file_tpl = 'user/frm_user';
    return $tpl->get_main($file_tpl, 2);
    
    
}

//Function get template for get list user
function get_listuser() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();
    $file_tpl = 'user/list_user';
    $listuser = $tpl->get_main($file_tpl, 2);

    //Create a params is array to get a block in list_user.html file
    $list_user['list_user'] = $tpl->get_block($listuser, 'list_user', 1);
    //Create a $sql query for get all total user 
    $sql = "SELECT COUNT(*) FROM az_users";
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];


    //Create params for pager
    $scroll_page = 5;
    $per_page = 8;
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $url = 'index.php?page=';

    //Setup a page
    $pager->page_set($url, $totalRecord, $scroll_page, $per_page, $current_page);

    //Create $sql query for get record of a page
    $sql_P = "SELECT * FROM az_users ORDER BY user_id ASC LIMIT " . $pager->start . "," . $pager->per_page;
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $account = $rows['username'];
            $name = $rows['name'];
            $address = $rows['address'];
            $email = $rows['email'];
            $phone = $rows['phone'];
            $date_create = $rows['date_create'];
            if ($rows['active'] == 0) {
                $active = 'No';
            } else if ($rows['active'] == 1) {
                $active = 'Yes';
            } else {
                $active = 'No';
            }
            $checkbox = '<input type="checkbox" name="userId[]" value="' . $rows['user_id'] . '"/>';
            $actions = '<a href="edit.php?userId=' . $rows['user_id'] . ' " title="Edit" class="icon-1 info-tooltip"></a>
                        <a href="action.php?act=3&userId=' . $rows['user_id'] . ' " title="Delete" class="icon-2 info-tooltip" onclick="return deletechecked();"></a>
                        <a href="action.php?act=4&userId=' . $rows['user_id'] . ' " title="Grant Role" class="icon-3 info-tooltip"></a>
                        <a href="action.php?act=5&userId=' . $rows['user_id'] . ' " title="Active" class="icon-4 info-tooltip"></a>';
            $html .= $tpl->get_vars($list_user['list_user'], array('check_box' => $checkbox, 'username' => $account, 'name' => $name,
                        'address' => $address, 'email' => $email, 'phone' => $phone, 'date_create' => $date_create,
                        'active' => $active, 'action' => $actions));
        }
        $result_P->free();
        //Assign $html
        $html = $tpl->assign_blocks_content($listuser, array('list_user' => $html));
        $html = $tpl->get_vars($html, array('page_par_left' => $pager->firt_page,
                    'page_left' => $pager->previous_page, 'page-info' => $pager->page_info,
                    'page-right' => $pager->next_page, 'page-far-right' => $pager->last_page));
        return $html;
    } else {
        $result_P->free();
        $html = "<center><i>Không có người sử dụng nào trong hệ thống</i></center>";
        return $html;
    }
}

?>
